<?php
/**
 * 控制器基础类，所有控制器均需继承此类
 * @author chenfenghua <843958575@qq.com>
 * version v2.0
 */

namespace app\web\components;

use Yii;
use yii\web\Controller;
use app\web\modules\cmp\models\AppCmp;

class WebController extends Controller
{
    public $layout = '@app/web/views/layouts/main';
    public $pageSize = 10;
    public $web_url;
    public static $CODE_SUC = 200;
    public static $CODE_ERR = 400;
    public $data = [
        'js'=>[],
        'css'=>[] ,
        'flag'=>0,
        'footer'=>1,
        'meta'=>[
            'title'=>'chindor_kpi',
            'keywords'=>['name'=>'keywords','content'=>'chindor_kpi'],
            'description'=>['name'=>'description','content'=>'chindor_kpi']
        ],
        'body'=>['class'=>'', 'id'=>'']
    ];
    public $member = ['member_id'=>0, 'avatar'=>'', 'account'=>''];

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub

        $this->web_url = Yii::$app->params['web_url'];
        //$identity = Yii::$app->session['identity'];
        //$url = $this->web_url . '/home/default/index.html';

        //验证是否登陆
        if ($this->id != 'user') {
            $this->checkLogin();
        }
        //$this->checkLogin();

        // if(isset(Yii::$app -> session['identity']) && Yii::$app -> session['identity'] == 1)
        // {
        //     Yii::$app -> session['counts'] = AppCmp::get_every_count(Yii::$app -> session['user_id']);
        // }        
        
    }

    /**
     * 是否是手机号码
     *
     * @param $mobile
     * @return bool
     */
    public function isMobile($mobile)
    {
        return preg_match("/1[34578]{1}\d{9}$/",$mobile)? true: false;
    }

    /**
     * 是否登陆
     *
     * @param $mobile
     * @return
     */
    public function checkLogin()
    {
        //var_dump(Yii::$app->session['identity']);
        //var_dump($this->module->id);exit;

        //未登录只能查看home或各端登录注册页面
        // if(!Yii::$app->session['identity'] && $this->module->id != 'home' ){
        //     $url = $this->web_url . '/home/default/index.html';
        //     $this->redirect($url)->send();
        // }
        //已登陆企业端不能查找职位/猎头
        // if(Yii::$app->session['identity'] == 1 && ($this->module->id == 'head' || $this->module->id == 'owner')){
        //     $url = $this->web_url . '/cmp/subject/index.html';
        //     $this->redirect($url)->send();
        // }
        //已登陆猎头端不能查看企业端/求职者端
        // if(Yii::$app->session['identity'] == 2 && ($this->module->id == 'cmp' || $this->module->id == 'owner')){
        //     $url = $this->web_url . '/home/job/index.html';
        //     $this->redirect($url)->send();
        // }
        //已登陆求职者端不能查看企业端/猎头端
        // if(Yii::$app->session['identity'] == 3 && ($this->module->id == 'cmp' || $this->module->id == 'head')){
        //     $url = $this->web_url . '/home/job/index.html';
        //     $this->redirect($url)->send();
        // }
        // return true;
    }

    /**
     * [filter_data 用於显示的函数]
     * @param  [type] $data [一维数组]
     * @param  [type] $key  [键]
     * @return [type]       [没有该键返回空 ，又返回值]
     */
    public static function filter_data($data,$key)
    {   
        if(!empty($data)){
            if(!empty($data[$key])){
                return $data[$key];
            }else{
                return '';
            }
        }
    }

}